package com.ruoyi.file.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ruoyi.file.domain.FileType;
import com.ruoyi.file.api.domain.SysFile;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * FastDFS 文件存储
 * 
 * @author ruoyi
 */
@Service
public class FastDfsSysFileServiceImpl extends AbstractIFileService
{
    /**
     * 域名或本机访问地址
     */
    @Value("${fdfs.domain}")
    public String domain;

    @Autowired
    private FastFileStorageClient storageClient;


    @Override
    protected FileType fileType() {
        return FileType.FASTDF;
    }

    /**
     * FastDfs文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    protected void uploadFile(MultipartFile file, SysFile fileInfo) throws Exception {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        fileInfo.setSource(fileType().name());// 设置文件来源

        String url = domain + "/" + storePath.getFullPath();
        fileInfo.setUrl( url);
        fileInfo.setPath( url);
    }

    @Override
    protected boolean deleteFile(SysFile fileInfo) {
        return false;
    }
}
